<?php
class Shop_Form_Product_Add extends Promotor_Form_Scaffold {
	public function init() {
		$this->setMethod('post');
		$this->setAttrib('enctype', Zend_Form::ENCTYPE_MULTIPART);

		$this->addPrefixPath('Promotor_Form_Decorator_','Promotor/Form/Decorator', self::DECORATOR);
        
		$subForm = $this->_getContentForm();
        $this->addSubForm($subForm, 'contenttab');

        // attrybuty tylko wtedy gry są
        if (null !== ($subForm = $this->_getAttrForm()))
        {
        	$this->addSubForm($subForm, 'attribs');
        }
        
        $subForm = $this->_getDescriptionForm();
        $this->addSubForm($subForm, 'descriptiontab');
        
        $subForm = $this->_getCategoryForm();
        $this->addSubForm($subForm, 'categorytab');
        
        $subForm = $this->_getImageForm();
        $this->addSubForm($subForm, 'imagetab');
        
        $subForm = $this->_getSeoForm();
        $this->addSubForm($subForm, 'seotab');
        
        $subForm = $this->_getExForm();
        $this->addSubForm($subForm, 'exttab');
	}
	

	/**
     * @return Zend_Dojo_Form_SubForm
     */
    protected function _getContentForm() {
    	$subForm = new Zend_Form_SubForm();
        $subForm->setAttribs(array(
            'name'   => 'contenttab',
            'legend' => 'Produkt',
        ));
        $subForm
        	->addElement(
                'text',
                'name',
                array(
                    'label' => 'Nazwa produktu',
                    'required'  => true,
                )
            )
            ->addElement(new KontorX_Form_Element_Db_Select('manufacturer_id', array(
            	'label' => 'Producent',
                'description' => 'Wybierz producenta produktu',
            	'required' => true,
            	'tableName' => 'shop_manufacturer',
            	'tableCols' => array('key' => 'id','value' => 'name')
            )))
            ->addElement(
                'text',
                'price',
                array(
                    'label' => 'Cena produktu',
                    'required'  => true,
                	'validators' => array(
                		'float' => 'Float'
                	)
                )
            )
            ->addElement(
                'text',
                'quantity',
                array(
                    'label' => 'Ilość dostępnych produktów',
                    'required'  => false,
                	'validators' => array(
                		'float' => 'Int'
                	)
                )
            );

        return $subForm;
    }
    
	/**
     * @return Zend_Dojo_Form_SubForm
     */
    protected function _getAttrForm() {
    	$pk = $this->getPrimaryKey();
    	$rq = $this->getRequest();
    	
        $model = new Shop_Model_AttrsSet();

        $attrSetId = $rq->getParam('attr_set_id');
        $attrset = $model->getSetById($attrSetId, $pk);

        // brak zdefiniowanych atrybutów, pomijam
        if (null === $attrset) {
        	return;
        }

        $subForm = new Zend_Form_SubForm();
        $subForm->setAttribs(array(
            'name'   => 'attribs',
            'legend' => 'Atrybuty',
        ));
        
        $subForm->addElement('hidden','attr_set_id', array(
        	'value' => $attrSetId,
        	'required' => false,
        ));

        foreach ($attrset as $attr)
        {
        	// domyślne opcje
        	$options = array(
        		'label' => $attr['name'],
        		'value' => isset($attr['value']) ? $attr['value'] : $attr['default'],
        		'required' => (bool) $attr['required'],
        		'description' => $attr['description'],
        	);

        	// utwórz element w zalerzności od typu
        	switch ($attr['type'])
        	{
        		case 'INT':
        			$type = 'text';
        			$options['validators'] = array(
        				'int' => 'Int'
        			);
        			break;

        		case 'TEXT':
        			$type = 'text';
        			break;

        		default:
        			// typ nie istnieje kontynuuj foreach
        			continue 2;
        	}
        	
        	// dodaj nowy element
        	$subForm->addElement($type, $attr['attr_id'], $options);
        }

        return $subForm;
    }
    
	/**
     * @return Zend_Form_SubForm
     */
    protected function _getDescriptionForm() {
    	$subForm = new Zend_Form_SubForm();
        $subForm->setAttribs(array(
            'name'   => 'descriptiontab',
            'legend' => 'Opis',
        ));

		$description = new Zend_Form_Element_Textarea('description', array(
			'label'        => 'Opis produktu',
			'inheritWidth' => 'true',
            'required' 	   => true
		));
		$description->addDecorator(new KontorX_Form_Decorator_TinyMCE());
		$subForm->addElement($description);
		
		$summary = new Zend_Form_Element_Textarea('summary', array(
			'label'        => 'Streszczenie opisu prodiktu',
        	'description' => 'Krótki opis o produkcie',
			'inheritWidth' => 'true',
            'required' 	   => false
		));
		$summary->addDecorator(new KontorX_Form_Decorator_TinyMCE());
		$subForm->addElement($summary);

        return $subForm;
    }

	/**
     * @return Zend_Form_SubForm
     */
    protected function _getImageForm() {
    	$subForm = new Zend_Form_SubForm();
        $subForm->setAttribs(array(
            'name'   => 'imagetab',
            'legend' => 'Zdięcia',
        ));

		$subForm
			->addElement(new KontorX_Form_Element_File('image_main', array(
        		'label' => 'Zdjęcie główne',
				'description' => 'Zdjęcie główne reprezentuje produkt w spisie',
        		'required' => false,
        		'destination' => PUBLIC_PATHNAME . '/' . Shop_Model_Image::UPLOAD_PATH,
        		'filters' => array(
        			'UniqName' => new KontorX_Filter_File_UniqName()
        		),
        		'validators' => array(
        			'upload' => array(
        				'validator' => 'Upload'),
//        			'isImage' => array(
//        				'validator' => 'IsImage',
//        				'options' => array(
//        					'headerCheck' => true
//        				)),
//        			'mimeType' => array(
//        				'validator' => 'MimeType',
//        				'options' => array(
//        					'headerCheck' => true,
//        					'image/jpeg',
//        					'image/png',
//        					'image/gif',
//        				)
//        			),
					'extension' => array(
        				'validator' => 'Extension',
        				'options' => 'jpg,png,gif'
        			),
        			'size' => array(
        				'validator' => 'Size',
        				'options' => array(
        					'min' => '3kb',
        					'bytestring' => true)),
        			'count' => array(
        				'validator' => 'Count',
        				'options' => array(
        					'max' => 1))))))
        	->addElement(new KontorX_Form_Element_File('image1', array(
        		'label' => 'Zdjęcie 1',
        		'required' => false,
        		'destination' => Shop_Model_Image::UPLOAD_PATH,
        		'filters' => array(
        			'UniqName' => new KontorX_Filter_File_UniqName()
        		),
        		'validators' => array(
        			'upload' => array(
        				'validator' => 'Upload'),
//        			'isImage' => array(
//        				'validator' => 'IsImage',
//        				'options' => array(
//        					'headerCheck' => true
//        				)),
//        			'mimeType' => array(
//        				'validator' => 'MimeType',
//        				'options' => array(
//        					'headerCheck' => true,
//        					'image/jpeg',
//        					'image/png',
//        					'image/gif',
//        				)
//        			),
					'extension' => array(
        				'validator' => 'Extension',
        				'options' => 'jpg,png,gif'
        			),
        			'size' => array(
        				'validator' => 'Size',
        				'options' => array(
        					'min' => '3kb',
        					'bytestring' => true)),
        			'count' => array(
        				'validator' => 'Count',
        				'options' => array(
        					'max' => 1))))))
        	->addElement(new KontorX_Form_Element_File('image2', array(
        		'label' => 'Zdjęcie 2',
        		'description' => 'Zdjęcie uzupełniające, posiłkowe dopełnia produkt - pokazuje np. produkt z innej perspektywy',
        		'required' => false,
        		'destination' => Shop_Model_Image::UPLOAD_PATH,
        		'filters' => array(
        			'UniqName' => new KontorX_Filter_File_UniqName()
        		),
        		'validators' => array(
        			'upload' => array(
        				'validator' => 'Upload'),
//        			'isImage' => array(
//        				'validator' => 'IsImage',
//        				'options' => array(
//        					'headerCheck' => true
//        				)),
//        			'mimeType' => array(
//        				'validator' => 'MimeType',
//        				'options' => array(
//        					'headerCheck' => true,
//        					'image/jpeg',
//        					'image/png',
//        					'image/gif',
//        				)
//        			),
					'extension' => array(
        				'validator' => 'Extension',
        				'options' => 'jpg,png,gif'
        			),
        			'size' => array(
        				'validator' => 'Size',
        				'options' => array(
        					'min' => '3kb',
        					'bytestring' => true)),
        			'count' => array(
        				'validator' => 'Count',
        				'options' => array(
        					'max' => 1))))));

        		

        	

        	

        return $subForm;
    }

	/**
     * @return Zend_Form_SubForm
     */
    protected function _getCategoryForm() {
    	$subForm = new Zend_Form_SubForm();
        $subForm->setAttribs(array(
            'name'   => 'categorytab',
            'legend' => 'Kategoria',
        ));
        $tree = new KontorX_Form_Element_JsTree('tree', array(
			'label'        => 'Kategoria',
            'required' 	   => false,
        	'registerInArrayValidator' => false,

        	'jsOptions' => array(
        		'data' => array(
        			'url' => 'shop/category/list/format/jsTree',
        			'async' => false
        		)
        	)
		));
		$subForm->addElement($tree);
		
        return $subForm;
    }
    
    /**
     * @return Zend_Form_SubForm
     */
    protected function _getSeoForm() {
    	$subForm = new Zend_Form_SubForm();
        $subForm->setAttribs(array(
            'name'   => 'metatab',
            'legend' => 'Pozycjonowanie'
        ));
        $subForm
        	->addElement(
                'text',
                'alias',
                array(
                    'label' => 'Alias produktu',
                    'required'  => true,
                    'regExp' => '[\w\d\-\s]+',
                    'invalidMessage' => 'Tylko tekst bez polskich znaków diakrytycznych!',
                	'filters' => array(
                		'strtrim' => new Zend_Filter_StringTrim(),
                		'strtolower' => new Zend_Filter_StringToLower(),
                		'rewrite' => new KontorX_Filter_Word_Rewrite()
                	),
                	'validators' => array(
                		'db_no_exsists' => new Zend_Validate_Db_NoRecordExists(
                			'shop_product', 'alias', array(
                				'field' => 'id',
                				'value' => current($this->getPrimaryKey())))
                	)
                )
            )
            ->addElement(
                'Textarea',
                'meta_keywords',
                array(
                    'label'      => 'Słowa kluczowe',
                    'trim'       => true,
                	'validators' => array(
                		'strlen' => new Zend_Validate_StringLength(10, 90)
                	)
				)
            )
            ->addElement(
                'Textarea',
                'meta_description',
                array(
                    'label'      => 'Opis strony',
                    'trim'       => true,
                	'validators' => array(
                		'strlen' => new Zend_Validate_StringLength(10, 90)
                	)
                )
            );
            
        return $subForm;
    }
    
	/**
     * @return Zend_Form_SubForm
     */
    protected function _getExForm() {
    	$subForm = new Zend_Form_SubForm();
        $subForm->setAttribs(array(
            'name'   => 'exttab',
            'legend' => 'Zaawansowane'
        ));
        $subForm
            ->addElement(
            	'checkbox',
            	'publicated',
            	array(
            		'label' => 'Publikować',
            		'checked' => true
            	)
            );
            
        return $subForm;
    }
}